'use strict';
import './style.scss';
var $ = require('jquery')
var ABConvert = require('./convert')
// randomly using ES7 object rest spread because it currently raises
// an error in all browsers, but can be transpiled by Babel
const { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 };
const n = { x, y, ...z };
if (Object.keys(n).map((key) => n[key]).reduce((p,v) => p + v) === 10) {
  document.querySelector('#app').insertAdjacentHTML('afterbegin', '<h1>works.</h1>');
}


var _ = require('lodash')
const { diff, addedDiff, deletedDiff, detailedDiff, updatedDiff } = require("deep-object-diff");
var deepdiff = require('deep-diff')
var observableDiff = require('deep-diff').observableDiff;
var applyChange = require('deep-diff').applyChange;
var revertChange = require('deep-diff').revertChange
import u from './utils'
var codegen = u.codegen
console.log(require('./utils'),u)
const code1 = require('./code1')
const code2 = require('./code2')

const firmata = require('./firmata')
// console.log(firmata)
var cparse = u.cparse
var lhs = cparse(code1, {
  types: ["void", "boolean", "char", "short", "int", "long", "float", "double",
   'Servo', 'DpBoard', 'DpSensor', 'DpActuator','byte', 'SerialFirmata'],
  modifier: []
})
var rhs = cparse(code2, {
  types: ["void", "boolean", "char", "short", "int", "long", "float", "double",
   'Servo', 'DpBoard', 'DpSensor', 'DpActuator','byte', 'SerialFirmata'],
  modifier: []
})
// var firmataAst = cparse(firmata, {
//   types: ["void", "boolean", "char", "short", "int", "long", "float", "double",
//    'Servo', 'DpBoard', 'DpSensor', 'DpActuator','byte', 'SerialFirmata'],
//   modifier: []
// })
// // console.log(JSON.stringify(ast))
console.log(lhs)
console.log(rhs)
// console.log(codegen(lhs))
// console.log(codegen(rhs))
// console.log(firmataAst)
// console.log(deepdiff(lhs, rhs))
// console.log(difference(lhs,rhs))
var l = _.filter(lhs, {name: "setup"})[0].body
var r = _.filter(rhs, {name: "setup"})[0].body
var changes = observableDiff(r, l, function (d) {
  // Apply all changes except to the name property...
  // if (d.path && d.path[d.path.length - 1] !== 'pos') {
    if (d.path) revertChange(r, l, d);
    console.log(d)
//   // }
//   if (d.kind !=='E') {
// applyChange(lhs, rhs, d);
//   }

});

console.log(codegen(l))
console.log(changes)

function difference(object, base) {
	function changes(object, base) {
		return _.transform(object, function(result, value, key) {
			if (!_.isEqual(value, base[key])) {
				result[key] = (_.isObject(value) && _.isObject(base[key])) ? changes(value, base[key]) : value;
			}
		});
	}
	return changes(object, base);
}
// console.log(lhs, rhs)
// var setup = _.filter(lhs, {name: "setup"})
// console.log(setup[0].body, codegen(setup[0].body))
// console.log(codegen(lhs))
// console.log(res)

var mainWorkspace;

document.addEventListener("DOMContentLoaded", function() {
	var blockly_div = document.getElementById('blocklyDiv');
	// var toolbox_div = document.createElement('div');
	//Loads toolbox for JavaScript
	// toolbox_div.innerHTML = Blocklify.JavaScript.toolbox;
	// document.body.appendChild(toolbox_div);
	mainWorkspace = Blockly.inject(blockly_div,
          {
            // toolbox: toolbox_div,
            toolbox: document.getElementById('toolbox'),
            media: "./media/",
            zoom: {
              controls: true,
              wheel: true,
              startScale: 1.0,
              maxScale: 3,
              minScale: 0.3,
              scaleSpeed: 1.2},
          });

  // charges the example code
  document.getElementById('code').value = exampleCode;
  $('#parse_code').click(parse_code)
  $('#parse_blocks').click(parse_blocks)
});

var delete_all_blocks = function() {
	mainWorkspace.getTopBlocks().forEach(function (el) {
		el.dispose();
	});
};

var parse_code = function () {
	delete_all_blocks();
	var javascript_code = document.getElementById('code').value;
  var abc = new ABConvert('Arduino')
	var xmlDom = abc.codeToDom(javascript_code, 'mixed');
  console.log(xmlDom)
	Blockly.Xml.domToWorkspace(xmlDom, mainWorkspace);
};

var parse_blocks = function () {
	var output = document.getElementById('code');
  output.value = Blockly.Arduino.workspaceToCode(mainWorkspace);
  // output.value = Blocklify.JavaScript.Generator.workspaceToCode(mainWorkspace);
};

var exampleCode = code1
window.workspace = mainWorkspace
